package com.liukai.bishe.ui.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;

import com.bumptech.glide.Glide;
import com.liukai.bishe.R;
import com.liukai.bishe.ui.activity.MainActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

public class ShowBigPhoto {

    private Context mContext;
    private final AlertDialog mDialog;
    private final ImageView mImageView;

    public ShowBigPhoto(Context context,String imgPath,String type) {
        mContext = context;
        mDialog = new AlertDialog.Builder(mContext).create();
        View view = View.inflate(mContext, R.layout.dialog_show_big_photo,null);
        mDialog.setContentView(view);

        mImageView = mDialog.findViewById(R.id.my_image);
        if (type.equals("本地")){
            mImageView.setImageResource(R.mipmap.img1);
        }else{
            Glide.with(mContext).load(imgPath).into(mImageView);
        }
        //大图的点击事件（点击让他消失）
        mImageView.setOnClickListener(v -> mDialog.dismiss());

        //大图的长按监听
        mImageView.setOnLongClickListener(v -> {
            //弹出的“保存图片”的Dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
            builder.setItems(new String[]{mContext.getResources()
                            .getString(R.string.save_picture)},
                    (dialog, which) -> saveCroppedImage(((BitmapDrawable) mImageView
                            .getDrawable()).getBitmap()));
            builder.show();
            return true;
        });


    }

    //保存图片
    private void saveCroppedImage(Bitmap bmp) {
        File file = new File("/sdcard/myFolder");
        if (!file.exists())
            file.mkdir();

        file = new File("/sdcard/temp.jpg".trim());
        String fileName = file.getName();
        String mName = fileName.substring(0, fileName.lastIndexOf("."));
        String sName = fileName.substring(fileName.lastIndexOf("."));

        // /sdcard/myFolder/temp_cropped.jpg
        String newFilePath = "/sdcard/myFolder" + "/" + mName + "_cropped" + sName;
        file = new File(newFilePath);
        try {
            file.createNewFile();
            FileOutputStream fos = new FileOutputStream(file);
            bmp.compress(Bitmap.CompressFormat.JPEG, 50, fos);
            fos.flush();
            fos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    //动态的ImageView
    private ImageView getImageView(){
        ImageView iv = new ImageView(mContext);
        //宽高
        iv.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        //设置Padding
        iv.setPadding(20,20,20,20);
        //imageView设置图片
        @SuppressLint("ResourceType") InputStream is = mContext.getResources().openRawResource(R.mipmap.ic_launcher);
        Drawable drawable = BitmapDrawable.createFromStream(is, null);
        iv.setImageDrawable(drawable);
        return iv;
    }

    public void show(){
        mDialog.show();
    }


}
